'use strict'
// 添加按钮点击事件
function Insert() {
    window.location.href = '/Home/Add';
}

// 修改按钮点击事件
function Update(id) {
    window.location.href = `/Home/Add/${id}`;
}

// 删除按钮点击事件
function Delete(id) {
    if (confirm("确认删除吗？")) {
        window.location.href = `/Home/Delete/${id}`;
    } else {
        alert("你取消了删除！")
    }
}

// 保存按钮点击事件
function Save() {
    let id = $('[name=Id]').val();
    let name = $('[name=NewName]').val();
    let supplier = $('[name=NewSupplier]').val();
    let price = $('[name=NewPrice]').val();
    let stock = $('[name=NewStock]').val();
    let remarks = $('[name=NewRemarks]').val();
    let obj = {
        id, name, supplier, price, stock, remarks
    };
    $.post('/Home/Save', obj, function () {
        alert("保存成功！");
        window.location.href = '/Home/Index';
    })
}

// 取消按钮点击事件
function Return() {
    window.location.href = '/Home/Index';
}

// 搜索按钮点击事件
function Select() {
    let text = $('[name=Text]').val();
    let obj = {
        text
    };
    $.post('/Home/Select', obj, function (res) {
        console.log(res);
        $('.tbrow').html('');
        res.forEach(item => {
            let html = `
                <tr class="tbrow">
                    <td>${item.id}</td>
                    <td>${item.productName}</td>
                    <td>${item.supplier}</td>
                    <td>${item.price}</td>
                    <td>${item.stock}</td>
                    <td>${item.remarks}</td>
                    <td>
                        <input type="button" value="修改" onclick="Update(${item.id})">
                        <input type="button" value="删除" onclick="Delete(${item.id})">
                    </td>
                </tr>
            `
            $('.tb').append(html);
        });
    })
}